<?php

namespace app\speedDating\controller\member;

use app\speedDating\AppController;
use app\speedDating\services\common\JWTAuthUserService;
use app\speedDating\services\MemberService;
use app\speedDating\web\JsonResult;
use Psr\SimpleCache\InvalidArgumentException;

class MemberController extends AppController
{
    /**
     * 当前登录用户详情
     * @param MemberService $service
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws InvalidArgumentException
     */
    public function detail(MemberService $service)
    {
        return JsonResult::success($service->detail());
    }

    /**
     * 修改用户信息
     * @param MemberService $service
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update(MemberService $service)
    {
        $data['nickname'] = request()->param('nickname');
        $data['picture'] = request()->param('picture');

        $this->validate(
            $data,
            [
                'nickname' => 'max:20',
                'picture' => 'max:180',
            ],
            [
                'nickname.max' => '昵称长度限制20字符',
                'picture.max' => '头像长度限制180字符',
            ]
        );

        $service->update($data, JWTAuthUserService::user()->id);
        return JsonResult::success();
    }
}